# @FileName  : conf.py
# @Time      : 2025/3/6 04:46
# @Author    : LuZhaoHui
# @Software  : PyCharm

from configparser import ConfigParser
from vars import *


class MyParser(ConfigParser):
    # Inherit from built-in class: ConfigParser
    def optionxform(self, option_str):
        # Rewrite without lower()
        return option_str


class FilmConf:
    # 配置
    varConf = {
        'Title': '冲洗服务',
        'WindowMax': False,
        'WindowWidth': 960,
        'WindowHeight': 720,
        'StartPrompt': False,
        'PromptText': '请确认冲洗罐位置',
        'PromptImage': 'prompt.jpg',
    }
    varCom = {
        'ComPort': '',
        'ComRate': 115200,
        'LogSave': True,
        'LogRows': 1000
    }
    varSetup = {
        'LinkCheck': False,
        'LinkMode': 0,
        'OriginCoord': [0.0, 0.0, 0.0],
        'Point1Coord': [0.0, 0.0, 0.0, 0.0],
        'Point2Coord': [0.0, 0.0, 0.0, 0.0],
        'Point3Coord': [0.0, 0.0, 0.0, 0.0],
        'Point4Coord': [0.0, 0.0, 0.0, 0.0],
        'Point5Coord': [0.0, 0.0, 0.0, 0.0],
        'Point6Coord': [0.0, 0.0, 0.0, 0.0],
        'Point7Coord': [0.0, 0.0, 0.0, 0.0],
        'ArmRightMode': True,
        'ArmRightPoint': [2, 3],
        'ArmLeftMode': False,
        'ArmLeftPoint': [5, 6],
        'QuakeSpace': 20,
        'J4': [3000, 3000],
        'Rate': [3000, 3000]
    }
    varCode = {
        'CodeConfRows': 30,
        'CodeConfWord': '标准',
        'CodeList': [],
        'CodeAct': '',
        'CodePara': 0,
        'CodeMoveRate': 0,
        'CodeMoveRateF': False,
        'CodeMoveSpace': 0,
        'CodeDelay': 200,
        'CodeOut': 1,
        'CodeJ4Step': 0,
        'CodeJ4Time': 0,
        'CurCodeFile': '',
        'CodeTimer': 300,
        'CodeActDelay': 1000,
    }
    varUi = {
        'DebugEnable': True,
        'SetupEnable': True,
        'InfoEnable': False,
        'ActState': ['', '', ''],
        'CurWin': 0,
        # 'ButtonBorder': 1,
        # 'ButtonColor': [225, 225, 225],
        # 'ButtonBorderColor': [173, 173, 173],
        # 'ButtonHoverColor': [229, 241, 251],
        # 'ButtonHoverBorderColor': [0, 120, 215],
        # 'TextBackColor0': [250, 240, 230],
        # 'TextBackColor1': [255, 255, 255],
        'ActPointBackColor0': [229, 241, 251],
        'ActPointBackColor1': [229, 241, 251],
        'ActPointBackColor2': [229, 241, 251],
        'ButtonPointEnable': [True, True, True, True, True, True, True],
        'ButtonPointText': ['1桶', '2桶', '3桶', '4桶', '5桶', '6桶', '7桶'],
        'ButtonActEnable': [True, True, True, True, True],
        'ButtonActText': ['动作1', '动作2', '动作3', '动作4', '动作5']
    }
    vars = {
        'CONF': varConf,
        'COM': varCom,
        'SETUP': varSetup,
        'CODE': varCode,
        'UI': varUi,
    }

    def __init__(self, file):
        self.config = MyParser()
        self.name = file
        self.read()

    def get(self, section, key):
        return self.config.get(section, key)

    def read(self):
        self.config.read(self.name, 'utf-8')
        # 取配置
        for key, vs in self.vars.items():
            # print("%s:%s" % (key, vars))
            for k, v in vs.items():
                # print("%s:%s" % (key, k))
                s = self.get(key, k)
                if isinstance(vs[k], bool):
                    vs[k] = bool(eval(s))
                elif isinstance(vs[k], int):
                    vs[k] = int(s)
                elif isinstance(vs[k], float):
                    vs[k] = float(s)
                elif isinstance(vs[k], list):
                    sl = s.split(',')
                    for i, sv in enumerate(sl):
                        if len(vs[k]) <= i:
                            vs[k].append('')
                        if isinstance(vs[k][i], bool):
                            vs[k][i] = bool(eval(sv))
                        elif isinstance(vs[k][i], int):
                            vs[k][i] = int(sv)
                        elif isinstance(vs[k][i], float):
                            vs[k][i] = float(sv)
                        else:
                            vs[k][i] = sv
                else:
                    vs[k] = s

    def writeFile(self):
        with open(self.name, 'w', encoding='utf-8') as f:
            self.config.write(f)

    def write(self, section, key, value):
        self.config.set(section, key, str(value))
        self.writeFile()
